<?php

namespace MeetUP\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Base\Controllers\Controller as BaseController;


class Controller extends BaseController
{
    //七牛地址图片
    protected $url = 'http://paz3fjvaa.bkt.clouddn.com/';
    //存放树结构数据
    protected $tree = [];
    protected $views_path="meetup/";
    public function __construct()
    {

    }

    /**
     * 渲染模版
     *
     * @param string $path 模版名称
     * @param array $data  赋值模版变量
     * @return void
     */
    protected function view(string $path,array $data=[]){
        return view($this->views_path.$path,$data);
    }

    /**
     * treetoary 归递
     * @access protected
     *
     * @param array  $list 要转换的数据集
     * @param string $level level标记字段
     * @param string $keyName
     *
     * @return array
     */


    protected function treetoary(array $list, int $level = 0, string $keyName = 'name'): array
    {
        foreach ($list as $key => $val) {

            $tmp_str = str_repeat("|----------", $level);
            if ($tmp_str == null) {
                $val[$keyName] = $val[$keyName];
            } else {
                $val[$keyName] = $tmp_str . $val[$keyName];
            }
            if (!array_key_exists('_child', $val)) {
                array_push($this->tree, $val);
            } else {
                $tmp_ary = $val['_child'];
                unset($val['_child']);
                array_push($this->tree, $val);
                $this->treetoary($tmp_ary, $level + 1, $keyName);
            }
        }
        return $this->tree;
    }

    /**
     * 把返回的数据集转换成Tree
     * @access protected
     *
     * @param array  $list 要转换的数据集
     * @param string $pid parent标记字段
     * @param string $level level标记字段
     *
     * @return array
     */
    protected function list_to_tree(array $list, string $pk = 'id', string $pid = 'pid', string $child = '_child', int $root = 0): array
    {
        // 创建Tree
        $tree = array();
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] = &$list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[] = &$list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent = &$refer[$parentId];
                        $parent[$child][] = &$list[$key];
                    }
                }
            }
        }
        return $tree;
    }

    /**
     * 对查询结果集进行排序
     * @access protected
     *
     * @param array  $list 查询结果
     * @param string $field 排序的字段名
     * @param array  $sortby 排序类型
     * asc正向排序 desc逆向排序 nat自然排序
     *
     * @return array
     */
    protected function list_sort_by(array $list, string $field, string $sortby = 'asc'): array
    {

        $refer = $resultSet = array();
        foreach ($list as $i => $data)
            $refer[$i] = &$data[$field];
        switch ($sortby) {
            case 'asc': // 正向排序
                asort($refer);
                break;
            case 'desc':// 逆向排序
                arsort($refer);
                break;
            case 'nat': // 自然排序
                natcasesort($refer);
                break;
        }
        foreach ($refer as $key => $val)
            $resultSet[] = &$list[$key];
        return $resultSet;

    }

    /**
     * 在数据列表中搜索
     * @access protected
     *
     * @param array $list 数据列表
     * @param mixed $condition 查询条件
     * 支持 array('name'=>$value) 或者 name=$value
     *
     * @return array
     */
    protected function list_search(array $list, $condition): array
    {
        if (is_string($condition))
            parse_str($condition, $condition);
        // 返回的结果集合
        $resultSet = array();
        foreach ($list as $key => $data) {
            $find = false;
            foreach ($condition as $field => $value) {
                if (isset($data[$field])) {
                    if (0 === strpos($value, '/')) {
                        $find = preg_match($value, $data[$field]);
                    } elseif ($data[$field] == $value) {
                        $find = true;
                    }
                }
            }
            if ($find)
                $resultSet[] = &$list[$key];
        }
        return $resultSet;
    }

    //递归分组
    protected function getTree($data, $pId = '0')
    {
        $tree = [];
        foreach($data as $k => $v)
        {
            if($v['c_parentid'] == $pId)
            {
                $v['c_parentid'] = $this->getTree($data, $v['c_id']);
                $tree[] = $v;
                unset($data[$k]);
            }
        }
        return $tree;
    }
    //时间显示多久之前(21分钟之前发表)
    public function formatDate($time = 0){
        $time = strtotime($time);
        $tmp = time() - $time;
        if($tmp && $tmp <= 60 * 60){
            return ceil($tmp / 60) + 1 . '分钟前';
        }elseif($tmp > 60 * 60 && $tmp < 60 * 60 * 24){
            return ceil($tmp / 60 / 60) + 1 . '小时前';
        }elseif($tmp > 60 * 60 * 24 && $tmp < 60 * 60 * 24 * 30){
            return ceil($tmp / 60 / 60 / 24) + 1 . '天前';
        }else{
            return date('Y-m-d H:i:s',$time);
        }
    }
    //base64图片上传 七牛
    public function uploadImg(Request $request)
    {

        $base64 = preg_replace("/\s/",'+',$request->input('img'));
        $img = base64_decode($base64);
        $disk = \Storage::disk('qiniu'); //使用七牛云上传
        $img_name = date('Y/m/d-H:i:s-') . uniqid() . '.png';
        $filename = $disk->put($img_name,$img);//上传
        if(!$filename) {
            return $this->apiResponse(500, '上传失败');
        }
        $img_url = $disk->getDriver()->downloadUrl($img_name); //获取下载链接
        return $this->apiResponse(200, '上传成功',$img_url);
    }

}
